package ch.ifage.business.jpa.dom;

import java.util.Iterator;

import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

import org.junit.Test;

import ch.ifage.business.dom.FirstStep;
import ch.ifage.business.dom.Step;
import ch.ifage.business.service.Workflow;
import ch.ifage.business.service.mock.AnalyseFluxData;

/**
 * 
 * @author Christian
 * 
 */
public class LoadDbWithMock extends AbstractDBTest {
	
	/**
	 * Alimentation de la DB avec le mock si elle est vide.
	 */
	@Test
	public final void loadDB() {
		CriteriaBuilder qb = getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Step> c = qb.createQuery(Step.class);
		Query query = getEntityManager().createQuery(c);
		Integer nbSteps = query.getResultList().size();		
		if (nbSteps == 0) {
			AnalyseFluxData mock = new AnalyseFluxData();
			getTrx().begin();
			for (Iterator<Workflow> i = mock.getList().iterator(); i.hasNext();) {
				FirstStep fs = (FirstStep) i.next().getFirstStep();
				getEntityManager().persist(fs);
			}
			getTrx().commit();
		}

	}

}
